package com.firmye.codereposboot.service;

import com.alibaba.druid.support.json.JSONUtils;
import com.firmye.codereposboot.dao.ProductDao;
import com.firmye.codereposboot.model.commons.Month;
import com.firmye.codereposboot.model.commons.echarts.EchartsResponse;
import com.firmye.codereposboot.model.commons.echarts.SeriesData;
import com.firmye.codereposboot.model.product.pojo.Product;
import com.firmye.codereposboot.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class ProductService {

    @Autowired
    private ProductDao productDao;

    public Boolean cuProduct(Product product){
        Date now = new Date();

        if(null == product.getProId()){
            product.setShelfTime(now);
            product.setUpdatedTime(now);

            return 1 == productDao.insert(product);
        }else{
            product.setUpdatedTime(now);

            return 1 == productDao.update(product);
        }
    }

    public int delProducts(String proIds) {
        return productDao.dels(proIds);
    }

    public EchartsResponse getPie() {
        EchartsResponse rep = new EchartsResponse();
        List pieLegend = new ArrayList();
        List pieSeries = new ArrayList();

        List<Product> pros = productDao.queryAll();
        for(Product product:pros){
            pieLegend.add(product.getProName());

            SeriesData ser = new SeriesData();
            ser.setName(product.getProName());
            ser.setValue(product.getInStock());
            pieSeries.add(ser);
        }

        rep.setPieLegend(pieLegend);
        rep.setPieSeries(pieSeries);

        return rep;
    }

    public List<Product> getProducts() {
        return productDao.queryAll();
    }

    public EchartsResponse getBar(Integer year, Integer proId) {
        EchartsResponse resp = new EchartsResponse();
        List<Integer> barSeries = new ArrayList<>();

        List<Month> months = DateUtil.getMonths(year);
        for(Month month:months){
            Date begin = DateUtil.getMonthStart(DateUtil.getMonth(year,month.getMonth()));
            Date end = DateUtil.getMonthStart(DateUtil.getMonth(year,month.getMonth() + 1));

            Integer sv = productDao.querySV(begin,end,proId);
            barSeries.add(sv);
        }

        resp.setBarSeries(barSeries);
        return resp;
    }

    public EchartsResponse getLine(Integer year, Integer month, String proIds) {
        EchartsResponse resp = new EchartsResponse();

        List legend = new ArrayList();
        Date date = DateUtil.getMonth(year,month);
        Month m = DateUtil.getMonth(date);
        List xAxis = new ArrayList();
        xAxis.addAll(m.getDates());
        List series = new ArrayList();

        List<Integer> proIdList = (List) JSONUtils.parse(proIds);

        for(Integer proId:proIdList){
            Map map = new HashMap<>();

            Product pro = productDao.queryById(proId);
            legend.add(pro.getProName());

            map.put("name",pro.getProName());
            map.put("type","line");
            List markPoint = new ArrayList();
            Map mpMax = new HashMap();
            mpMax.put("type","max");
            mpMax.put("name","最大值");
            markPoint.add(mpMax);
            Map mpMin = new HashMap();
            mpMin.put("type","min");
            mpMin.put("name","最小值");
            markPoint.add(mpMin);
            Map mpMap = new HashMap();
            mpMap.put("data",markPoint);
            map.put("markPoint",mpMap);
            List markLine = new ArrayList();
            Map ml = new HashMap();
            ml.put("type","average");
            ml.put("name","平均值");
            markLine.add(ml);
            Map mlMap = new HashMap();
            mlMap.put("data",markLine);
            map.put("markLine",mlMap);

            List data = new ArrayList();
            for(Integer day:m.getDates()){
                Date begin = DateUtil.getDayStart(DateUtil.getDay(date,day));
                Date end = DateUtil.getDayStart(DateUtil.getDay(date,day + 1));

                Integer sv = productDao.querySV(begin,end,proId);
                data.add(sv);
            }
            map.put("data",data);

            series.add(map);
        }

        resp.setLegend(legend);
        resp.setXAxis(xAxis);
        resp.setSeries(series);

        return resp;
    }
}
